package com.jic.market.controller.manage;

import com.jic.common.base.vo.RestResult;
import com.jic.market.request.largeTurntableTemplate.LargeTurntableTemplateRequest;
import com.jic.market.response.LargeTurntableTemplateResponse;
import com.jic.market.response.PageResponse;
import com.jic.market.service.LargeTurntableTemplateService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * description: LargeTurntableTemplateController
 * date: 2020/7/7 16:29
 * author: wangys
 * version: 1.0
 */
@RestController
@RequestMapping("/largeTurntableTemplate")
@Slf4j
public class LargeTurntableTemplateController {

    @Autowired
    LargeTurntableTemplateService largeTurntableTemplateService;

    @ApiOperation(value = "查询大转盘模板列表（分页）")
    @PostMapping(value = "/listByPage")
    public RestResult<PageResponse<LargeTurntableTemplateResponse>> listByPage(@RequestBody LargeTurntableTemplateRequest request) {
      return RestResult.success(largeTurntableTemplateService.listByPage(request));
    }

    @ApiOperation(value = "查询大转盘模板列表")
    @PostMapping(value = "/findAll")
    public RestResult<List<LargeTurntableTemplateResponse>> findAll(@RequestBody LargeTurntableTemplateRequest request) {
        return RestResult.success(largeTurntableTemplateService.findAll(request));
    }

    @ApiOperation(value = "查询大转盘模板详情")
    @PostMapping(value = "/detail")
    public RestResult<LargeTurntableTemplateResponse> detail(@RequestBody LargeTurntableTemplateRequest request) {
        return RestResult.success(largeTurntableTemplateService.detail(request));
    }

    @ApiOperation(value = "删除大转盘模板")
    @PostMapping(value = "/delete")
    public RestResult<Long> delete(@RequestBody LargeTurntableTemplateRequest request) {
        return RestResult.success(largeTurntableTemplateService.delete(request));
    }


    @ApiOperation(value = "新增或者更新大转盘模板")
    @PostMapping(value = "/createOrUpdate")
    public RestResult<Long> createOrUpdate(@RequestBody @Valid LargeTurntableTemplateRequest request) {
        return RestResult.success(largeTurntableTemplateService.createOrUpdate(request));
//        try {
//            return RestResult.success(largeTurntableTemplateService.createOrUpdate(request));
//        } catch (DuplicateKeyException e) {
//            log.error("largeTurntableTemplate createOrUpdate error", e);
//            return RestResult.error("9999", "模板名称重复");
//        }  catch (Exception e) {
//            log.error("largeTurntableTemplate createOrUpdate error", e);
//            return RestResult.error("9999", "系统异常");
//        }
    }
}
